package com.muyi.biling.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;

import com.dothantech.lpapi.LPAPI;
import com.dothantech.printer.IDzPrinter;
import com.google.zxing.activity.CaptureActivity;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import com.muyi.biling.R;
import com.muyi.biling.bluetooth.BluetoothLeService;
import com.muyi.biling.bluetooth.DeviceScanActivity_;
import com.muyi.biling.util.Utils;

/**
 * Created by developer on 18-3-5.
 */


@EActivity(R.layout.activity_printer_qr)
public class PrinterQRActivity extends AppCompatActivity {
    private final int QR_SCAN_CODE = 1;

    @ViewById(R.id.printer_btn_bluetooth)
    Button btnBlueTooth;
    @ViewById(R.id.printer_btn_scan)
    Button btnScan;
    private LPAPI mLPInstance;
    private String mAddress;

    @AfterViews
    void initView() {

    }

    @Click(R.id.printer_btn_bluetooth)
    void toBlueTooth() {
        Intent intent = new Intent(this, DeviceScanActivity_.class);
        startActivityForResult(intent, BluetoothLeService.RESULT_CODE);
    }

    @Click(R.id.printer_btn_scan)
    void toScanQR() {
        Intent intent = new Intent(this, CaptureActivity.class);
        startActivityForResult(intent, QR_SCAN_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == BluetoothLeService.RESULT_CODE) {
            if (data.getExtras() != null) {
                mAddress = data.getStringExtra("address");
                if (!TextUtils.isEmpty(mAddress)) {
                    mLPInstance = LPAPI.Factory.createInstance();
                    mLPInstance.openPrinterByAddress(new IDzPrinter.PrinterAddress(mAddress));
                }
            }
        }else if (requestCode == QR_SCAN_CODE && resultCode == CaptureActivity.RESULT_CODE_QR_SCAN) {
            Bundle bundle = data.getExtras();
            String scanResult = bundle.getString(CaptureActivity.INTENT_EXTRA_KEY_QR_SCAN);
            if (mLPInstance != null && !TextUtils.isEmpty(scanResult)) {
                mLPInstance.startJob(40, 40, 0);
                mLPInstance.drawRectangle(0, 0, 40, 40, 0.5);
                boolean b = mLPInstance.draw2DQRCode(scanResult, 5, 5, 30);
                mLPInstance.commitJob();
            }else {
                Utils.showToastShort("打印机未连接");
            }

        }
    }

    @Override
    protected void onDestroy() {
        if (mLPInstance != null && mLPInstance.isPrinterOpened()) {
            mLPInstance.cancel();
            mLPInstance.closePrinter();
            mLPInstance.quit();
        }
        super.onDestroy();
    }
}
